package com.sinai.mshab.client.ui.validate;

import com.sinai.mshab.client.util.Answer;
import com.sinai.mshab.client.ui.basic.BasicFormComponent;

public abstract class Validator {

	/**
	 * An abstract method that needs to be overwritten by every sub
	 * <code>Validator</code> since each validator validates differently.
	 * 
	 * @param component
	 *            The <code>QuestionComponent</code> who's answers shall be
	 *            validated.
	 * @return <code>true</code> if the answer is valid, <code>false</code>
	 *         otherwise.
	 */
	abstract public boolean validate(BasicFormComponent component);

	/**
	 * A generic validator that validates the answers in <code>component</code>
	 * using the regular expression <code>regex</code>.
	 * 
	 * @param component
	 *            The <code>QuestionComponent</code> who's answers shall be
	 *            validated.
	 * @param regex
	 *            A <code>Pattern</code> containing a compiled regex.
	 * @return true if component is valid and false otherwise.
	 */
	public boolean validate(BasicFormComponent component, String regex) {

		boolean valid = false;

		Answer an = component.getAnswer();
		String answer = an.getFirstAnswer();
		return answer.matches(regex);
	}
}
